﻿using System;
using System.ComponentModel;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
using DevExpress.XtraBars;
using DevExpress.ExpressApp;
using Hdt.TransOcean.Dal;
using System.Windows.Forms;

namespace Hdt.TransOcean.Presentation
{
    public partial class FormItem : DevExpress.ExpressApp.Win.Templates.XtraFormTemplateBase, ISupportClassicToRibbonTransform
    {
        private EOperation _operation;


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CheckTransformToRibbon();
        }
        protected override IModelFormState GetFormStateNode()
        {
            if (View != null)
            {
                return TemplatesHelper.GetFormStateNode(View.Id);
            }
            else
            {
                return base.GetFormStateNode();
            }
        }
        public override void SetSettings(IModelTemplate modelTemplate)
        {
            base.SetSettings(modelTemplate);
            formStateModelSynchronizerComponent.Model = GetFormStateNode();
        }
        [Obsolete("Use the DetailViewForm() constructor"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public FormItem(XafApplication application) : this() { }
        public FormItem()
        {
            InitializeComponent();
        }
        public FormItem(EOperation operation, string code = null)
        {
            InitializeComponent();
            this._operation = operation;
            txtCode.Enabled = false;
            Presentation.DisplayButtonByOperation(hdtManageControl1,operation);
            if (operation == EOperation.New)
            {
                //txtCode.Text = Dao.GetAutoCode("Customer");
                //chkUsed.Enabled = false;
            }
            if (operation == EOperation.Edit)
            {
                if (!String.IsNullOrEmpty(code))
                {
                    Item item = Dao.SelectItem(code)[0];

                    //txtCode.Text = customer.code;
                    //txtAddress.Text = customer.address;
                    //txtName.Text = customer.name;
                    //txtEmail.Text = customer.email;
                    //txtDescription.Text = customer.description;
                    //txtFax.Text = customer.fax;
                    //txtDebtDayNum.Text = customer.debt_day_num.ToString();
                    //rdoType.EditValue = customer.type;
                    //txtMobile.Text = customer.mobile;
                    //txtTaxCode.Text = customer.tax_code;
                    //txtTelephone.Text = customer.telephone;
                    //chkUsed.EditValue = customer.used;
                }
            }
        }

        private void hdtManageControl1_HdtNewButtonClick()
        {

        }

        private void hdtManageControl1_HdtSaveButtonClick()
        {
            string message = "Lưu thành công!";
            //bool flag = true;
            Dao dao = new Dao();
            Item item = new Item();
            //item.code = txtCode.Text;

            item.type = Int32.Parse(rdoType.Properties.Items[rdoType.SelectedIndex].Value.ToString());

            item.code = gridLookUpEditJourney.EditValue.ToString();

            switch (item.type)
            { 
                case 1:
                case 2:
                    item.name = gridLookUpEditAirline.Text;
                    break;

                case 3:
                    item.name = txtName.Text;
                    break;
            }
            
            item.airline_code = gridLookUpEditAirline.EditValue.ToString();
            item.journey_code = gridLookUpEditJourney.EditValue.ToString(); 
            item.purchase_price = Decimal.Parse(txtPurchasePrice.EditValue.ToString());
            item.purchase_commision = Decimal.Parse(txtPurchaseCommision.EditValue.ToString()); 
            item.sale_price = Decimal.Parse(txtSalePrice.EditValue.ToString());
                        
            try
            {
                if (Presentation.ValidItem(item) == true)
                    Dao.InsertItem(item);
            }
            catch (Exception e)
            {
             //   flag = false;
                message = e.ToString();
                
            }
            finally
            {
                MessageBox.Show(message, "Vé - Lưu");
            }
        }

        private void hdtManageControl1_HdtEditButtonClick()
        {

        }

        private void hdtManageControl1_HdtDeleteButtonClick()
        {

        }

        private void hdtManageControl1_HdtCloseButtonClick()
        {

        }

        private void hdtManageControl1_HdtImportButtonClick()
        {

        }

        private void hdtManageControl1_HdtExportButtonClick()
        {

        }

        private void FormItem_Load(object sender, EventArgs e)
        {
           
            // Load Customer
            gridLookUpEditJourney.Properties.DataSource = Dao.SelectJourney();
            gridLookUpEditJourney.Properties.DisplayMember = "name";
            gridLookUpEditJourney.Properties.ValueMember = "code";
            gridColumnJourneyCode.FieldName = "code";
            gridColumnJourneyName.FieldName = "name";
            gridColumnJourneyDescription.FieldName = "description";


            gridLookUpEditAirline.Properties.DataSource = Dao.SelectAirline();
            gridLookUpEditAirline.Properties.DisplayMember = "name";
            gridLookUpEditAirline.Properties.ValueMember = "code";
            gridColumnAirlineCode.FieldName = "code";
            gridColumnAirlineName.FieldName = "name";
            gridColumnAirlineDescription.FieldName = "description";
        }
        
    }
}
